package com.dvt.faceScan;
import android.Manifest;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.os.Build;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.AdapterView;
import android.widget.GridView;
import android.widget.SimpleAdapter;

import com.baidu.idl.face.platform.FaceSDKManager;
import com.dvt.faceScan.widget.Config;

import java.util.ArrayList;
import java.util.HashMap;


public class MainActivity extends Activity {
    private GridView gridview;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
                WindowManager.LayoutParams.FLAG_FULLSCREEN);
        setContentView(R.layout.activity_main);
        initLib();
        requestPermissions(99, Manifest.permission.CAMERA);
        gridview = (GridView) findViewById(R.id.gridView1);
        ArrayList<HashMap<String, Object>> lstImageItem = new ArrayList<HashMap<String, Object>>();
        for(int i = 1;i<6;i++){
            HashMap<String,Object> map = new HashMap<String, Object>();
            if(i==1){
                map.put("ItemImage", R.drawable.grzl);
                map.put("ItemText", "个人资料");
                lstImageItem.add(map);
            }else if(i==2){
                map.put("ItemImage", R.drawable.gzcx);
                map.put("ItemText", "工资查询");
                lstImageItem.add(map);
            }else if(i==3){
                map.put("ItemImage", R.drawable.rlqd);
                map.put("ItemText", "人脸签到");
                lstImageItem.add(map);
            }else if(i==4){
                map.put("ItemImage", R.drawable.rlqt);
                map.put("ItemText", "人脸签退");
                lstImageItem.add(map);
            }else if(i==5){
                map.put("ItemImage", R.drawable.kqgl);
                map.put("ItemText", "考勤管理");
                lstImageItem.add(map);
            }
        }
        SimpleAdapter saImageItems = new SimpleAdapter(this,
                lstImageItem, R.layout.activity_main2,
                new String[]{"ItemImage", "ItemText"},
                new int[]{R.id.ItemImage, R.id.ItemText});
        gridview.setAdapter(saImageItems);
        gridview.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
                                    long arg3) {
                int index = arg2 + 1;// id是从0开始的，所以需要+1
                if (index == 1) {
                    Intent intent = new Intent(MainActivity.this, employeeActivity.class);
                    startActivity(intent);
                }
                if (index == 2) {
                    Intent intent = new Intent(MainActivity.this, Gzcx.class);
                    startActivity(intent);
                }
                if (index == 3) {
                    Intent intent = new Intent(MainActivity.this, FaceActivity.class);
                    intent.putExtra("id", "2");
                    intent.putExtra("in", "0");
                    startActivity(intent);
                }
                if (index == 4) {
                    Intent intent = new Intent(MainActivity.this, FaceActivity.class);
                    intent.putExtra("id", "2");
                    intent.putExtra("in", "1");

                    startActivity(intent);
                }
                if (index == 5) {
                    Intent intent = new Intent(MainActivity.this, KqglActivity.class);
                    startActivity(intent);
                }

            }
        });
    }

    //设置人脸扫描的配置
    /**
     * 初始化SDK
     */
    private void initLib() {
        // 为了android和ios 区分授权，appId=appname_face_android ,其中appname为申请sdk时的应用名
        // 应用上下文
        // 申请License取得的APPID
        // assets目录下License文件名
        FaceSDKManager.getInstance().initialize(this, Config.licenseID, Config.licenseFileName);
//        setFaceConfig();
    }
    //设置权限，允许相机权限
    public void requestPermissions(int requestCode, String permission) {
        if (permission != null && permission.length() > 0) {
            try {
                if (Build.VERSION.SDK_INT >= 23) {
                    // 检查是否有权限
                    int hasPer = checkSelfPermission(permission);
                    if (hasPer != PackageManager.PERMISSION_GRANTED) {
                        // 是否应该显示权限请求
                        boolean isShould = shouldShowRequestPermissionRationale(permission);
                        requestPermissions(new String[]{permission}, requestCode);
                    }
                } else {

                }
            } catch (Exception ex) {
                ex.printStackTrace();
            }
        }
    }

//    @Override
//    public void onBackPressed() {
//        super.onBackPressed();
//        AlertDialog.Builder dialog = new AlertDialog.Builder(MainActivity.this);
//        dialog.setTitle("Dialog").setIcon(android.R.drawable.ic_dialog_info).setMessage("弹出框").setPositiveButton("确定", new DialogInterface.OnClickListener() {
//            @Override
//            public void onClick(DialogInterface dialog, int which) {
//                finish();
//            }
//        }).setNegativeButton("取消", new DialogInterface.OnClickListener() {
//            public void onClick(DialogInterface dialog, int which) {
//                dialog.cancel();//取消弹出框
//            }
//        }).create().show();
//    }

}